﻿using System;
using System.Collections.Generic;
 
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using ExtAspNet;
using System.Text;

namespace HY.WEB.SA
{
    public partial class companyall : PageBase
    {

        public string stype="";
        public string us,um,bh,ro;
        public string tab = "company";
        public string whe = "", whe2 = "",whe3 = "",whe4 = "";
        public string ubh;
       
        public int pgCount = 0;

        protected void Page_Load(object sender, EventArgs e)
        {
            us = Session["ID"].ToString();
            um = Session["user"].ToString();
            ubh = Session["bh"].ToString();     
            um = Session["user"].ToString();
            ro = Session["role"].ToString();
            
            //whe = " cbh in (select cbh from company_fav where sbh='" + bh + "')";
            whe = "";
            //取组名

            string gnm = BLL.Bllbase.check_rol(ubh);
            if (gnm != "" & ro != "系统管理员")
            {
                whe3 = " and cbh in (select cbh from company_fav where sbh in(select sbh from sys_user where dgroup=''" + gnm + "'' ))";
                whe4 = " and cbh in (select cbh from company_fav where sbh in(select sbh from sys_user where dgroup='" + gnm + "' ))";
            }
            if (gnm == "" & ro != "系统管理员")
            {
                ExtAspNet.Alert.ShowInParent("无权限查看此页，将登出系统!", "提示", ExtAspNet.MessageBoxIcon.Information, "top.location.href='../default.aspx'");
            }
            //if (string.IsNullOrEmpty(ro) || ro != "系统管理员")
            //{
            //    ExtAspNet.Alert.ShowInParent("无权限查看此页，将登出系统!", "提示", ExtAspNet.MessageBoxIcon.Information, "top.location.href='default.aspx'");
            // }
            
            if (ViewState["tab"] != null)
            {
                if (ViewState["tab"].ToString() != "")
                    tab = ViewState["tab"].ToString();
            }
            else
                tab = "company";

            if (ViewState["whe"] != null)
            {
                if (ViewState["whe"].ToString() != "")
                    whe = ViewState["whe"].ToString();
            }
            else
                whe = "";

            if (ViewState["whe2"] != null)
            {
                if (ViewState["whe2"].ToString() != "")
                    whe2 = ViewState["whe2"].ToString();
            }
            else
                whe2 = "";


            pgCount = 0;
            Grid1.RecordCount = BLL.Bllbase.page_count(tab, whe2+whe4);
            pgCount = Grid1.RecordCount / Grid1.PageSize;
            if (Grid1.RecordCount % Grid1.PageSize != 0) pgCount += 1;

            lblcount.Text = "总记录:" + Grid1.RecordCount.ToString();

            if (!IsPostBack)
            {
                ViewState["tab"] = "";
                btdele.OnClientClick = Grid1.GetNoSelectionAlertReference("至少选择一项！");

                btclare.OnClientClick = Grid1.GetNoSelectionAlertReference("至少选择一项！");

          
                btnSearch.OnClientClick =  Window1.GetShowReference("../function/search.aspx?tab="+tab+"", "高级搜索");
      
                BindGrid();
                fill(); //填充查询
             
                pageset();

            }

            if (stype == "") stype= hidstype.Text;
        }

        //页面设置
        void pageset()
        {
            Grid1.RecordCount = BLL.Bllbase.page_count(tab, whe2 + whe4);

            pgCount = Grid1.RecordCount / Grid1.PageSize;
            if (Grid1.RecordCount % Grid1.PageSize != 0)
            {
                pgCount += 1;
            }
            Grid1.PageCount = pgCount;
            Grid1.PageIndex = 1;

            txtgo.Text = Grid1.PageIndex.ToString();
            lblpgid.Text = "当前页:" + Grid1.PageIndex.ToString();
            lblpgct.Text = "总页数:" + pgCount.ToString();
            lblcount.Text = "总记录:" + Grid1.RecordCount.ToString();
        }

        //取城市
        public static string getcity(string val)
        {
            DataTable dte = BLL.Bllbase.get_table("[pub_area]"," scode='"+val+"' ");
            if (dte.Rows.Count > 0)
                return dte.Rows[0]["svalue"].ToString();
            else
                return "";

        }

          private void fill()
          {

             DataTable d = BLL.BLLfun.get_search(tab);
             for (int i = 0; i < d.Rows.Count; i++)
                    {
                        ddlfiled.Items.Add(new ExtAspNet.ListItem(d.Rows[i][1].ToString(), d.Rows[i][0].ToString()));
                    }
               ddlfiled.DataBind();

               DataTable e = BLL.BLLfun.get_where();
               for (int i = 0; i < e.Rows.Count; i++)
               {
                   ddlwhere.Items.Add(new ExtAspNet.ListItem(e.Rows[i][0].ToString(), e.Rows[i][1].ToString()));
               }
                ddlwhere.DataBind();   ddlwhere.SelectedIndex = ddlwhere.Items.IndexOf(ddlwhere.Items.FindByValue("="));
            
        }

        private void BindGrid()
        {
            if (!string.IsNullOrEmpty(ViewState["tab"].ToString())) 
                tab = ViewState["tab"].ToString();
            
        
            DataTable table = BLL.Bllbase.get_table(tab, whe+whe3, "cbh desc", int.Parse(Grid1.PageIndex.ToString()), Grid1.PageSize);
           // DataTable table = BLL.Bllbase.get_table(tab, whe,"order by sid desc");           
            
            Grid1.DataSource = table;
            Grid1.DataBind();

        }

        //首页
        protected void btnf_Click(object sender, EventArgs e)
        {
            Grid1.PageIndex = 1;
            lblpgid.Text = "当前页:" + Grid1.PageIndex.ToString();
            BindGrid();

        }
        //尾页
        protected void btne_Click(object sender, EventArgs e)
        {
            Grid1.PageIndex = Grid1.PageCount;
            lblpgid.Text = "当前页:" + Grid1.PageIndex.ToString();
            BindGrid();

        }

        //上一页
        protected void btnpre_Click(object sender, EventArgs e)
        {
            if (Grid1.PageIndex > 2)
                Grid1.PageIndex = Grid1.PageIndex - 1;
            else
                Grid1.PageIndex = 1;
            lblpgid.Text = "当前页:" + Grid1.PageIndex.ToString();
            BindGrid();

        }

        //下一页
        protected void btnnext_Click(object sender, EventArgs e)
        {
            if (Grid1.PageIndex < Grid1.PageCount)
                Grid1.PageIndex = Grid1.PageIndex + 1;
            else
                Grid1.PageIndex = Grid1.PageCount;

            lblpgid.Text = "当前页:" + Grid1.PageIndex.ToString();

            BindGrid();

        }

        //到指定页
        protected void btngo_Click(object sender, EventArgs e)
        {

            if (VAN.IsInt(txtgo.Text))
                Grid1.PageIndex = int.Parse(txtgo.Text);
            else
                Grid1.PageIndex = 1;

            lblpgid.Text = "当前页:" + Grid1.PageIndex.ToString();

            BindGrid();

        }

        protected void Grid1_PageIndexChange(object sender, ExtAspNet.GridPageEventArgs e)
        {
            Grid1.PageIndex = e.NewPageIndex;
            BindGrid();
        }



        protected void Grid1_Sort(object sender, ExtAspNet.GridSortEventArgs e)
        {

            DataTable table = BLL.Bllbase.get_table(tab, whe+whe3, "cbh desc", int.Parse(Grid1.PageIndex.ToString()), Grid1.PageSize);   

            DataView view1 = table.DefaultView;
            view1.Sort = String.Format("{0} {1}", e.SortField, e.SortDirection);

            Grid1.DataSource = view1;
            Grid1.DataBind();

        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
          //  int val = Grid1.PageIndex * Grid1.PageSize;
            StringBuilder sb = new StringBuilder();
            foreach (int row in Grid1.SelectedRowIndexArray)
            {
                sb.Append(Grid1.DataKeys[row][0].ToString());
                sb.Append(",");
            }

            Alert.ShowInTop("你选择了取消收藏项：" + sb.ToString().TrimEnd(','));
           // HY.BLL.Bllbase.DeleteList(tab,sb.ToString().TrimEnd(','));
            BLL.BllSales.deletelistsalse(tab,sb.ToString().TrimEnd(','));
            BindGrid();
        }


        protected void btnclear_Click(object sender, EventArgs e)
        {
           // int val = Grid1.PageIndex * Grid1.PageSize;
            StringBuilder sb = new StringBuilder();
            foreach (int row in Grid1.SelectedRowIndexArray)
            {
                sb.Append(Grid1.DataKeys[row][0].ToString());
                sb.Append(",");
            }

            Alert.ShowInTop("你选择了清除简介数据：" + sb.ToString().TrimEnd(','));

            BLL.Bllbase.ClearList(tab, sb.ToString().TrimEnd(','));

            BindGrid();

        }


        protected void btnDelete2_Click(object sender, EventArgs e)
        {
           //int val = Grid1.PageIndex * Grid1.PageSize;
            StringBuilder sb = new StringBuilder();
            foreach (int row in Grid1.SelectedRowIndexArray)
            {
                sb.Append(Grid1.DataKeys[row][0].ToString());
                sb.Append(",");
            }

            Alert.ShowInTop("你选择了删除项：" + sb.ToString().TrimEnd(','));

            BLL.Bllbase.DeleteList(tab, sb.ToString().TrimEnd(','));

            BindGrid();
        }


        protected void Window1_Close(object sender, EventArgs e)
        {
            // Alert.ShowInTop("弹出窗口关闭了！");
           string key = txt.Text.Trim();
            tab = "company";
            try
            {
                if (key == "") whe = BLL.pub.pubwhere.get_where_sql(us, tab);
                pageset();
                BindGrid();

            }
            catch (Exception ex)
            {
                Alert.Show("查询条件错误");
            }
            finally
            {
                whe = "";
            }
            ViewState["whe2"] = whe2;
            ViewState["whe"] = whe;
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ttbSearchMessage_Trigger2Click(object sender, EventArgs e)
        {
            txt.ShowTrigger1 = true;
            string ddlf = ddlfiled.SelectedValue;
            string ddlw = ddlwhere.SelectedValue;
           string key = txt.Text.Trim();
            if (ddlw.Equals("="))
            {
                whe = " " + ddlf + " like  ''%" + key + "%'' ";
                whe2 = " " + ddlf + " like  '%" + key + "%' ";
            }
            else
            {
                if (ddlf == "sid" || ddlf == "编号")
                    whe = "" + ddlf + " " + ddlw + " " + key + "";
                else
                    whe = "" + ddlf + " " + ddlw + " '" + key + "'";

            }
            if (key == "")
            {
                whe = ""; whe2 = "";
            }
            pageset();
            BindGrid();
            ViewState["whe2"] = whe2;
            ViewState["whe"] = whe;
        }

        /// <summary>
        /// 清除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ttbSearchMessage_Trigger1Click(object sender, EventArgs e)
        {
            txt.Text = String.Empty;
            txt.ShowTrigger1 = false;
            BindGrid();
        }

        protected void btnall_Click(object sender, EventArgs e)
        {
            ViewState["tab"] = "company";
            tab = "company";
           
            Grid1.Title = "所有信息";



            Grid1.RecordCount = BLL.Bllbase.page_count(tab, whe2);

            int pgCount = Grid1.RecordCount / Grid1.PageSize;
            if (Grid1.RecordCount % Grid1.PageSize != 0)
            {
                pgCount += 1;
            }

            Grid1.PageIndex = 1;
            

            lblpgid.Text = "当前页:" + Grid1.PageIndex.ToString();
            lblpgct.Text = "总页数:" + pgCount.ToString();

            BindGrid();
            
        }

        protected void btndel_Click(object sender, EventArgs e)
        {
            ViewState["tab"] = "vi_comp_del";
            tab = "vi_comp_del";
            Grid1.Title = "信息回收站";
           


            Grid1.RecordCount = BLL.Bllbase.page_count(tab, whe2);

            int pgCount = Grid1.RecordCount / Grid1.PageSize;
            if (Grid1.RecordCount % Grid1.PageSize != 0)
            {
                pgCount += 1;
            }

            Grid1.PageIndex = 1;
          

            lblpgid.Text = "当前页:" + Grid1.PageIndex.ToString();
            lblpgct.Text = "总页数:" + pgCount.ToString();


            BindGrid();
        }

        }




    
}